<?php

class Core_Backend_Component_Design_Controller_Block extends Dcmp_Component_Controller {

    public function indexAction($integer_pageindex = 1, $integer_pagesize = 20, $sort = 'id', $dir = 'desc') {
        $this->getHistory()->record();
        $validator = $this->getRequest()->getValidator('filter');
        $collection = App::getModel('design/collection_block')
                        ->joinModule()
                        ->joinPageTemplate()
                        ->filterByArea($this->getSession()->get('managed_area_id'))
                        ->pagination($integer_pageindex, $integer_pagesize);
        if ($validator->isNotEmpty('name')) {
            $collection->filterByName($validator->getValue('name'));
        }
        if ($validator->isNotEmpty('pagetemplate')) {
            $collection->filterByPageTemplateId($validator->getValue('pagetemplate'));
        }
        $collection->sortBy($sort, $dir)->load();
        $records = $collection->loadCount();
        $pageTemplates = App::getModel('design/collection_page_template')
                        ->filterByAreaId($this->getSession()->get('managed_area_id'))
                        ->load();        
        return $this->viewResult(array('list' => $collection, 'records' => $records, 'page_templates'=>$pageTemplates));
    }

    public function chooseAction() {
        $modules = App::getModel('global/collection_module')
                    ->joinArea()
                    ->filterByAreaId($this->getSession()->get('managed_area_id'))
                    ->load();
        return $this->viewResult(array('modules' => $modules));
    }

    public function createAction($moduleid) {
        $module = App::getModel('global/module')->load($moduleid);
        $area = App::getModel('global/area')->load($this->getSession()->get('managed_area_id'));
        $moduleType = App::getModule($area->getNamespace().'/'.$module->getName(),$this->getComponent()->getDomain()->getName());
        $pageTemplates = App::getModel('design/collection_page_template')
                        ->filterByAreaId($area->getId())
                        ->filterByEnabled(1)
                        ->load();
        $positions = App::getModel('design/collection_page_position')
                        ->filterByAreaId($area->getId())
                        ->filterByEnabled(1)
                        ->load();
        $blockTemplates = App::getModel('design/collection_block_template')
                            ->filterByAreaId($this->getSession()->get('managed_area_id'))
                            ->filterByEnabled(1)
                            ->filterByModuleId($moduleid)
                            ->load();
        return $this->viewResult(array('module' => $module, 'area' => $area, 'module_type' => $moduleType,
            'page_templates'=>$pageTemplates, 'positions'=>$positions, 'block_templates'=>$blockTemplates));
    }

    public function editAction($id) {
        $model = App::getModel('design/block')->load($id);
        $this->getPage()->getNavigation()->addLabel($model->getName());
        return $this->createAction($model->getModuleId())->setModel($model);
    }

    public function sortAction($template, $position){
        $modules = App::getModel('design/collection_block')
                    ->filterByPageTemplateId($template)
                    ->filterByPosition($position)
                    ->load();
        return $this->viewResult(array('modules'=>$modules));
    }

    public function sortPostAction() {
        $sorting = $this->getRequest()->getForm('sorting');
        $table = App::getModel('design/block')->getTable();
        foreach ($sorting as $index => $id) {
            $table->updateSorting($id, $index + 1);
        }
        return $this->redirectResult($this->getHistoryUrl('design/block/index'));
    }

    public function applyPostAction() {
        return $this->saveModel(FALSE);
    }

    public function savePostAction() {
        return $this->saveModel();
    }

    private function saveModel($return = TRUE) {
        $data = $this->getRequest()->getForm('module');
        $model = App::getModel('design/block');
        if (isset($data['id']) && !empty($data['id'])) {
            $model->load($data['id']);
        }
//        $config = Kd_Loader::loadInstance($namespace.'_module_'.$this->getRequest()->getForm('modulename').'_config');
//        $data['parameters'] = $config->processParameters($data['parameters']);
        $model->addData($data);
        if (($errors = $model->validate() === TRUE)) {
            $model->save();
            $this->getMessager()->addInform($this->__('share.message.success'));
            return $return ?
                    $this->redirectResult($this->getHistoryUrl('design/block/index')) :
                    $this->redirectResult($this->getUrl(array('id' => $model->getIdValue()), 'edit'));
        } else {
            $this->getMessager()->addError($this->__('share.message.fail'))
                    ->addError($errors);
            return $this->createAction()->setModel($model);
        }
    }

}